package org.finesys.samples.netty.websocket.client;

import io.netty.buffer.Unpooled;
import org.finesys.common.netty.websocket.support.NettyChannelMap;
import org.finesys.common.netty.websocket.support.WebSocketEndpointConfig;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.stereotype.Component;

import javax.net.ssl.SSLException;
import java.io.UnsupportedEncodingException;

@Component
public class NettyWebSocketClientServer implements SmartInitializingSingleton {


    @Override
    public void afterSingletonsInstantiated() {
        try {
            start();
        } catch (SSLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws SSLException {
        start();
    }

    public static void start() throws SSLException {
        WebSocketEndpointConfig webSocketEndpointConfig = webSocketEndpointConfig();
        NettyWebSocketClient client = new NettyWebSocketClient(webSocketEndpointConfig);
        client.init();
        //发送数据
        sendMessage();
    }

    private static void sendMessage() {
        // 发送消息
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        for (int i = 0; i < 10; i++) {
            NettyChannelMap nettyChannelMap = NettyChannelMap.getInstance();
            nettyChannelMap.getChannelMap().forEach((key, value) -> {
                String message = "hello world";

                try {
                    value.getChannel().writeAndFlush(Unpooled.copiedBuffer(message.getBytes("UTF-8")));
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
            });
        }

    }

    private static WebSocketEndpointConfig webSocketEndpointConfig() {

        String logLevel = "debug";
        String host = "127.0.0.1";
        String port = "8001";
        String path = "/ws";
        String protocol = "wss";
        // 解析注解中的bossLoopGroupThreads属性
        int bossLoopGroupThreads = 1;
        // 解析注解中的workerLoopGroupThreads属性
        int workerLoopGroupThreads = 1;
        // 解析注解中的useCompressionHandler属性
        boolean useCompressionHandler = true;

        // 解析注解中的optionConnectTimeoutMillis属性
        int optionConnectTimeoutMillis = 1000;
        // 解析注解中的optionSoBacklog属性
        int optionSoBacklog = 1000;

        // 解析注解中的childOptionWriteSpinCount属性
        int childOptionWriteSpinCount = 1000;
        // 解析注解中的childOptionWriteBufferHighWaterMark属性
        int childOptionWriteBufferHighWaterMark = 1000;
        // 解析注解中的childOptionWriteBufferLowWaterMark属性
        int childOptionWriteBufferLowWaterMark = 0;
        // 解析注解中的childOptionSoRcvbuf属性
        int childOptionSoRcvbuf = 1000;
        // 解析注解中的childOptionSoSndbuf属性
        int childOptionSoSndbuf = 1000;
        // 解析注解中的childOptionTcpNodelay属性
        boolean childOptionTcpNodelay = true;
        // 解析注解中的childOptionSoKeepalive属性
        boolean childOptionSoKeepalive = true;
        // 解析注解中的childOptionSoLinger属性
        int childOptionSoLinger = 1000;
        // 解析注解中的childOptionAllowHalfClosure属性
        boolean childOptionAllowHalfClosure = true;

        // 解析注解中的readerIdleTimeSeconds属性
        int readerIdleTimeSeconds = 1000;
        // 解析注解中的writerIdleTimeSeconds属性
        int writerIdleTimeSeconds = 1000;
        // 解析注解中的allIdleTimeSeconds属性
        int allIdleTimeSeconds = 1000;

        // 解析注解中的maxFramePayloadLength属性
        int maxFramePayloadLength = 65536;

        // 解析注解中的useEventExecutorGroup属性
        boolean useEventExecutorGroup = true;
        // 解析注解中的eventExecutorGroupThreads属性
        int eventExecutorGroupThreads = 1;

        // 解析注解中的sslKeyPassword属性
        String sslKeyPassword = "";
        // 解析注解中的sslKeyStore属性
        String sslKeyStore = "";
        // 解析注解中的sslKeyStorePassword属性
        String sslKeyStorePassword = "";
        // 解析注解中的sslKeyStoreType属性
        String sslKeyStoreType = "";
        // 解析注解中的sslTrustStore属性
        String sslTrustStore = "";
        // 解析注解中的sslTrustStorePassword属性
        String sslTrustStorePassword = "";
        // 解析注解中的sslTrustStoreType属性
        String sslTrustStoreType = "";

        // corsOrigins
        Boolean corsAllowCredentials = true;

        // 解析注解中的corsOrigins属性
        String[] corsOrigins = {"*"};
        // 返回构建好的WebSocketEndpointConfig对象
        return new WebSocketEndpointConfig(host, port, path, bossLoopGroupThreads, workerLoopGroupThreads,
                useCompressionHandler, optionConnectTimeoutMillis, optionSoBacklog, childOptionWriteSpinCount,
                childOptionWriteBufferHighWaterMark, childOptionWriteBufferLowWaterMark, childOptionSoRcvbuf,
                childOptionSoSndbuf, childOptionTcpNodelay, childOptionSoKeepalive, childOptionSoLinger,
                childOptionAllowHalfClosure, readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds,
                maxFramePayloadLength, useEventExecutorGroup, eventExecutorGroupThreads, sslKeyPassword, sslKeyStore,
                sslKeyStorePassword, sslKeyStoreType, sslTrustStore, sslTrustStorePassword, sslTrustStoreType,
                corsOrigins, corsAllowCredentials, logLevel, protocol);
    }
}
